package bg.uni_sofia.conf_manager.beans;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import bg.uni_sofia.conf_manager.model.Conference;
import bg.uni_sofia.conf_manager.model.Employee;
import bg.uni_sofia.conf_manager.model.Lecturer;

@Stateless
@Path("/conferences")
public class ConferenceBean {
	
	@PersistenceContext
	private EntityManager em;

	@Inject
	LoginBean sesData; 
	
	public ConferenceBean() {
		super();
	}
	
	@GET
    @Path("/{id}")
    @Produces("application/json")
    public Conference getAllConferences(@PathParam("id") long id) {
    	TypedQuery<Conference> query = em.createNamedQuery("getAllConferences", Conference.class);
    	query.setParameter("id", id);
    	return query.getResultList().get(0);
    }
	
	@GET
    @Path("/{id}")
    @Produces("application/json")
    public Conference getConferencesByEmployee(@PathParam("id") long id) {
    	TypedQuery<Conference> query = em.createNamedQuery("getConferencesByEmployee", Conference.class);
    	query.setParameter("id", id);
    	return query.getResultList().get(0);
    }
	
	@POST
	@Consumes("application/json")
	public Conference addConference(Conference newConference) {
		List<Conference> confList = getAllConferences();
		for(Conference conf: confList){
			if (conf.getName().equals(newConference.getName())) {
				return null;
			}
		}
		TypedQuery<Employee> query = em.createNamedQuery("getEmployeeByName", Employee.class);
		query.setParameter("username", sesData.getUsername());
		Employee employee = query.getResultList().get(0);
		newConference.setEmployee(employee);
		newConference.setCreationDate(new Date());
		employee.addConference(newConference);
		em.persist(newConference);
		em.merge(employee);
		return newConference;
	}
	
	@GET
	@Produces("application/json")
	public List<Conference> getAllConferences() {
		return em.createNamedQuery("getAllConferences", Conference.class)
				.getResultList();
	}

	// public Company getCompanyById(@PathParam("id") long id) {
	// TypedQuery<Company> query = em.createNamedQuery("getCompanyById",
	// Company.class);
	// query.setParameter("id", id);
	// return query.getSingleResult();
	// }
	//
	@POST
	@Path("/getConferenceByName")
	@Consumes("application/json")
	public Conference getConferenceByName(String name) {
		TypedQuery<Conference> query = em.createNamedQuery(
				"getConferenceByName", Conference.class);
		query.setParameter("name", name);
		List<Conference> conf = query.getResultList();
		return conf.get(0);
	}
	
	@GET
    @Path("/checkConferenceStartTime/{conferenceName}")
    @Produces("application/json")
    public boolean checkConferenceStartTime(@PathParam("conferenceName") String conferenceName) {
    	Conference conference = getConferenceByName(conferenceName);
    	if(conference != null){
    		Date today = new Date();
	    	long timeDiff = conference.getStartTime().getTime() - today.getTime();
	    	long timeDiffDays = timeDiff/(1000*60*60*24);
	    	if(timeDiffDays > 30){
	    		return true;
	    	}else{
	    		return false;
	    	}
    	}else{
    		return false;
    	}
    }
}
